package com.iwinding.ladygg;

import android.support.multidex.MultiDexApplication;
import android.text.TextUtils;

import com.bugtags.library.Bugtags;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMOptions;
import com.hyphenate.easeui.controller.EaseUI;
import com.iwinding.ladygg.constant.Constant;
import com.iwinding.ladygg.modle.db.DBHelper;
import com.iwinding.ladygg.net.response.LoginRp;
import com.iwinding.ladygg.okhttputils.OkHttpUtils;
import com.iwinding.ladygg.utils.AMMapHelper;
import com.yayandroid.theactivitymanager.TheActivityManager;

import java.io.File;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import okhttp3.Cache;
import okhttp3.OkHttpClient;

/**
 * Created by Administrator on 2016/6/2.
 */
public class ICaneApplication extends MultiDexApplication implements AppForegroundStateManager.OnAppForegroundStateChangeListener {


    private LoginRp mLoginRp;

    public LoginRp getmLoginRp() {
        return mLoginRp;
    }

    public void setmLoginRp(LoginRp mLoginRp) {
        this.mLoginRp = mLoginRp;
        if(mLoginRp!=null && !TextUtils.isEmpty(mLoginRp.userId)){
            JPushInterface.setAlias(this, "icane_" + mLoginRp.userId, new TagAliasCallback() {
                @Override
                public void gotResult(int i, String s, Set<String> set) {
                    if(i==0){
                        //成功
                    }
                }
            });
        }

    }

    @Override
    public void onCreate() {
        super.onCreate();
        //BTGInvocationEventNone    // 静默模式，只收集 Crash 信息（如果允许，默认为允许）
        //BTGInvocationEventShake   // 通过摇一摇呼出 Bugtags
        //BTGInvocationEventBubble  // 通过悬浮小球呼出 Bugtags
        Bugtags.start("cdfed630a681f364ec5b3a268acc7d7b", this, Bugtags.BTGInvocationEventNone );
        CrashHandler.getInstance().init(getApplicationContext());
        AppForegroundStateManager.getInstance().addListener(this);
        initOkHttp();
        initDB();
        initLocation();
        initEM();
        initJpush();
        TheActivityManager.getInstance().configure(this);
        Fresco.initialize(this);
        ICaneFile.initICaneFolder();
    }

    /**
     * 初始化网络请求
     */
    private void initOkHttp() {
        File cacheFile = new File(this.getCacheDir(), "cache");
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("@hzy"))
//                .addInterceptor(new TokenInterceptor(this))
                .connectTimeout(Constant.TimeConstant.CONNETTIMEOUT, TimeUnit.MILLISECONDS)
                .readTimeout(Constant.TimeConstant.READTIMEOUT, TimeUnit.MILLISECONDS)
                .cache(cache)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }

    /**
     * 初始化数据库
     */
    private void initDB() {
        DBHelper.initDB(this.getApplicationContext());
    }

    /**
     * 启动定位服务，初始化定位数据
     */
    private void initLocation() {
        AMMapHelper.getInstance().init(getApplicationContext());
        AMMapHelper.getInstance().startLocation();
    }

    /**
     * 初始化环信
     */
    private void initEM() {
        EMOptions options = new EMOptions();
         // 默认添加好友时，是不需要验证的，改成需要验证
        options.setAcceptInvitationAlways(false);
        options.setAutoLogin(false);
//        //初始化
//        EMClient.getInstance().init(this, options);
        //在做打包混淆时，关闭debug模式，避免消耗不必要的资源
        EaseUI.getInstance().init(this,options);
        try {
            EMClient.getInstance().setDebugMode(true);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void initJpush(){
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
    }

    @Override
    public void onAppForegroundStateChange(AppForegroundStateManager.AppForegroundState newState) {
        if (AppForegroundStateManager.AppForegroundState.IN_FOREGROUND == newState) {//2分钟算应用内
            // App just entered the foreground. Do something here!
        } else {//超过2分钟算应用外
            // App just entered the background. Do something here!
        }
    }


}
